﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="LineRegion.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2008, 2009, 2010, 2011
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Particles.
//
//   starLiGHT.Particles is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Particles is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Particles. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Particles are available on request.
// </license>
// <original_license>
//  * FLINT PARTICLE SYSTEM
//  * .....................
//  * 
//  * Author: Richard Lord
//  * Copyright (c) Richard Lord 2008-2009
//  * http://flintparticles.org
//  * 
//  * 
//  * Licence Agreement
//  * 
//  * Permission is hereby granted, free of charge, to any person obtaining a copy
//  * of this software and associated documentation files (the "Software"), to deal
//  * in the Software without restriction, including without limitation the rights
//  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  * copies of the Software, and to permit persons to whom the Software is
//  * furnished to do so, subject to the following conditions:
//  * 
//  * The above copyright notice and this permission notice shall be included in
//  * all copies or substantial portions of the Software.
//  * 
//  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  * THE SOFTWARE.
// </original_license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev::                       $:  Revision of last commit *
// * $Author::                    $:  Author of last commit   *
// * $Date::                      $:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.ParticleSystem.twoD
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;
    using starLiGHT.ParticleSystem.Common;

    #endregion

    public class LineRegion : IRegion2D
    {
        private Vector2 _start;
        private Vector2 _end;
        private Vector2 _length;
        private System.Random random = new System.Random();

        public LineRegion()
            : this(Vector2.Zero, Vector2.Zero)
        {
        }

        public LineRegion(Vector2 start, Vector2 end)
        {
            this._start = start;
            this._end = end;
            this._length = this._end - this._start;
        }

        public Vector2 Start
        {
            get 
            {
                return this._start; 
            }

            set
            {
                this._start = value;
                this._length = this._end - this._start;
            }
        }

        public Vector2 End
        {
            get 
            {
                return this._end; 
            }

            set
            {
                this._end = value;
                this._length = this._end - this._start;
            }
        }

        public bool contains(Vector2 pos)
        {
            // not on line if dot product with perpendicular is not zero
            if ((pos.X - this._start.X) * this._length.Y - (pos.Y - this._start.Y) * this._length.X != 0)
            {
                return false;
            }

            // is it between the points, dot product of the vectors towards each point is negative
            return (pos.X - this._start.X) * (pos.X - this._end.X) + (pos.Y - this._start.Y) * (pos.Y - this._end.Y) <= 0;
        }

        public Vector2 getLocation()
        {
            Vector2 ret = this._start;
            float scale = (float)random.NextDouble();
            ret.X += this._length.X * scale;
            ret.Y += this._length.Y * scale;
            return ret;
        }

        public float getArea()
        {
            return this._length.Length();
        }
    }
}
